package com.lenovo.lcdm.type.vo;

import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @desc: 属性详情
 * @author:
 * @date:
 */
public class AttributeDetailView {

    private LocalAttributeModel localAttributeModel;
    private List<AbstractValueView> attributeValues;

    public AttributeDetailView(LocalAttributeModel localAttributeModel, List<? extends AbstractValueView> attributeValues) {
        this.localAttributeModel = localAttributeModel;
        this.attributeValues = (List<AbstractValueView>) attributeValues;
    }

    /**
     * Get attribute value, and return as String, if this attribute is multiple attribute, then this method will only
     * return first value
     * @return attribute values
     */
    public String getValue(){
        List<AbstractValueView> values = getMultipleValueViews();
        if (!CollectionUtils.isEmpty(values)){
            return values.get(0).getValue();
        }
        return null;
    }

    /**
     * Get multiple attribute values and return as List<String>
     * @return multiple attribute values
     */
    public List<String> getMultipleValues(){
        List<AbstractValueView> values = getMultipleValueViews();
        if (!CollectionUtils.isEmpty(values)){
            return values.stream().map(AbstractValueView::getValue).filter(Objects::nonNull).collect(Collectors.toList());
        }
        return new ArrayList<>();
    }

    /**
     * Get attribute original type value, user could to convert to related type if needed, if this attribute is multiple
     * attribute, then this method will only return first value
     * @return original type value
     */
    public Object getOriginalValue(){
        List<AbstractValueView> values = getMultipleValueViews();
        if (!CollectionUtils.isEmpty(values)){
            return values.get(0).getOriginalValue();
        }
        return null;
    }

    /**
     * Get multiple attribute original type values, user could to convert to related type if needed
     * @return multiple original type values
     */
    public List<Object> getOriginalMultipleValues(){
        List<AbstractValueView> values = getMultipleValueViews();
        if (!CollectionUtils.isEmpty(values)){
            return values.stream().map(AbstractValueView::getOriginalValue).filter(Objects::nonNull).collect(Collectors.toList());
        }
        return new ArrayList<>();
    }

    /**
     * Get attribute value view, if this attribute is multiple attribute, then this method will only return first value
     * @return attribute values
     */
    public AbstractValueView getValueView(){
        List<AbstractValueView> values = getMultipleValueViews();
        if (!CollectionUtils.isEmpty(values)){
            return values.get(0);
        }
        return null;
    }

    /**
     * Get multiple attribute value view
     * @return attribute value views
     */
    public List<AbstractValueView> getMultipleValueViews(){
        return attributeValues;
    }

    /**
     * Get local attribute model
     * @return local attribute model
     */
    public LocalAttributeModel getLocalAttributeModel() {
        return localAttributeModel;
    }


    public void setLocalAttributeModel(LocalAttributeModel localAttributeModel) {
        this.localAttributeModel = localAttributeModel;
    }

    public void setAttributeValues(List<AbstractValueView> attributeValues) {
        this.attributeValues = attributeValues;
    }
}
